package com.code.wflow.service.impl;

import com.code.wflow.mapper.WorkflowNodeMapper;
import com.code.wflow.model.WorkflowNode;
import com.code.wflow.service.WorkflowNodeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 流程节点表(WorkflowNode)表服务层
 *
 * @author ggh
 * @date 2022/10/26
 */
@Service
public class WorkflowNodeServiceImpl implements WorkflowNodeService {
    @Autowired
    private WorkflowNodeMapper workflowNodeMapper;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public WorkflowNode queryById(Long id) {
        return this.workflowNodeMapper.queryById(id);
    }

    /**
     * 通过workflowId查询数据
     *
     * @param workflowId 主键
     * @return 实例对象
     */
    @Override
    public List<WorkflowNode> queryByWorkflowId(Long workflowId) {
        return this.workflowNodeMapper.queryByWorkflowId(workflowId);
    }


    /**
     * 查询下一个节点
     *
     * @param workflowId
     * @param parentCode
     * @return
     */
    @Override
    public WorkflowNode queryNodeByCondition(Long workflowId, String parentCode) {
        return this.workflowNodeMapper.queryNodeByCondition(workflowId, parentCode);
    }

    /**
     * 新增数据
     *
     * @param workflowNode 实例对象
     * @return 实例对象
     */
    @Override
    public boolean insert(WorkflowNode workflowNode) {
        return this.workflowNodeMapper.insert(workflowNode) > 0;
    }

    /**
     * 新增数据
     *
     * @param workflowNodeList 实例对象
     * @return 实例对象
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean insertList(List<WorkflowNode> workflowNodeList, Long workflowId) {
        if (this.workflowNodeMapper.insertList(workflowNodeList, workflowId) > 0) {
            return true;
        }
        throw new RuntimeException();
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Long id) {
        return this.workflowNodeMapper.deleteById(id) > 0;
    }

    /**
     * 通过主键删除数据
     *
     * @param idList 主键
     * @return 是否成功
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteByIdList(List<Long> idList) {
        if (this.workflowNodeMapper.deleteByIdList(idList) == idList.size()) {
            return true;
        }
        throw new RuntimeException();
    }
}
